<template>
  <el-tabs v-model="activeName" @tab-click="handleClick">
    <el-tab-pane label="角色管理" name="first">
      <div class="dashboard-container">
        <div class="app-container">
          <el-card>
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click.native="addRolus"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column label="序号" align="center" type="index" width="120" />
              <el-table-column label="角色名称" align="center" width="240" prop="name" />
              <el-table-column label="描述" align="center" prop="description" />
              <el-table-column label="操作" width="240" fixed="right">
                <template #default="scoped">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click.native="editFn(scoped.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click.native="delFn(scoped.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--  <!-一分页器
              page-size用于控制每页多少条数据
              total用于控制分页分多少页
              current-page当前第几页
              layout分页器的布局
              hide-on-single-page只有一页是否隐藏
              current-change当页码改变的时候触发
              一一> -->
            <el-row>
              <el-pagination
                layout="prev, pager, next"
                :page-size="params.pagesize"
                :current-page="params.page"
                :total="total"
                @current-change="currentChangeFn"
              />
            </el-row>
          </el-card>
        </div>
        <!-- 弹窗 -->
        <el-dialog
          :title="title"
          :visible.sync="dialogShow"
          width="30%"
          @close="dialogClose"
        >
          <!-- 表单 -->
          <el-form ref="formRef" :rules="formRules" label-width="80px" :model="formData">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="formData.description" />
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogClose">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-tab-pane>
    <el-tab-pane label="公司信息" name="second">公司信息</el-tab-pane>
  </el-tabs>
</template>

<script>
import { getRolueList, addRolues, getRoulesInfo, updateRoulesInfo, delRoules } from '@/api'
export default {
  data() {
    return {
      activeName: 'first',
      roleList: [],
      params: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      dialogShow: false,
      formData: {
        name: '',
        description: ''
      },
      formRules: {
        name: { required: true, message: '角色名称不能为空', tigger: 'blur' },
        description: { required: true, message: '角色描述不能为空', tigger: 'blur' }
      },
      currentNode: {}
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑角色' : '新增角色'
    }
  },
  async created() {
    this.fetchRolueList()
  },
  methods: {
    // 删除
    delFn(val) {
      this.$confirm('确定要删除吗？此操作不可逆!', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoules(val.id)
        this.$message.success('删除成功')
        // 删除优化，，删除最后一页的最后一条数据时，自动跳转到前一页
        if (this.roleList.length === 1 && this.params.page !== 1) {
          this.params.page--
        }
        // 重新渲染页面
        this.fetchRolueList()
      }).catch(() => {
        this.$message.warning('取消删除')
      })
    },
    // 编辑按钮
    async editFn(val) {
      // console.log(val)
      this.currentNode = val
      // 根据id查询角色信息
      const { data } = await getRoulesInfo(val.id)
      // console.log(data)
      this.formData = data
      this.dialogShow = true
    },
    // 提交事件
    submit() {
      // 兜底校验
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        try {
          if (!this.formData.id) {
          // 验证通过调用接口
            await addRolues(this.formData)
          } else {
            await updateRoulesInfo(this.formData)
          }
        } catch (error) {
          this.dialogClose()
          return
        }
        // 提示用户
        this.$message.success(this.title + '成功')
        // 重新渲染页面
        this.fetchRolueList()
        // 关闭窗口，清空数据
        this.dialogClose()
      })
    },
    // 带击新增用户按钮
    addRolus() {
      this.dialogShow = true
    },
    // 关闭弹窗的方法
    dialogClose() {
      this.dialogShow = false
      this.$refs.formRef.resetFields()
      this.formData = {
        name: '',
        description: ''
      }
    },
    async fetchRolueList() {
      const { data } = await getRolueList(this.params)
      // console.log(res)
      this.roleList = data.rows
      this.total = data.total
    },
    // tab栏切换
    handleClick() {

    },
    // 页码改变时的回调函数，接受值为跳转后的页码
    currentChangeFn(val) {
      this.params.page = val
      // console.log(val)
      this.fetchRolueList()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
